package cn.com.xxlibrary.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.com.xxlibrary.dao.IBorrowDao;
import cn.com.xxlibrary.orm.BooksBean;
import cn.com.xxlibrary.orm.BookTypeBean;
import cn.com.xxlibrary.orm.BorrowBean;
import cn.com.xxlibrary.orm.ManagerInfoBean;
import cn.com.xxlibrary.util.UtilTools;

@Transactional
@Repository("borrowDao")
public class BorrowDaoImpl implements IBorrowDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	public List selManager() {
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM ManagerInfoBean WHERE manState=1";
		return session.createQuery(hql).list();
	}

	public void borrowBook(int bid, int uid) {
		Session session = sessionFactory.getCurrentSession();
		BooksBean book = (BooksBean) session.get(BooksBean.class, bid);
		ManagerInfoBean manager = (ManagerInfoBean) session.get(
				ManagerInfoBean.class, uid);
		book.setBookCount(book.getBookCount() - 1);
		book.setBookLoanCount(book.getBookLoanCount() + 1);
		session.update(book);
		BorrowBean borrow = new BorrowBean();
		borrow.setBorrowBook(book);
		borrow.setBorrowManager(manager);
		borrow.setBorrowTime(UtilTools.receiveNowTime());
		borrow.setIsReturn(0);
		borrow.setRenewTimes(0);
		session.save(borrow);
	}

	public Map bookOnLoan(int bid, int start, int pageSize, String wd) {
		Session session = sessionFactory.getCurrentSession();

		StringBuffer hql = new StringBuffer("FROM BorrowBean WHERE isReturn=0 ");

		if (bid != 0) {
			hql.append(" AND borrowBook.bookId=").append(bid);
		}

		if (UtilTools.isNotNullorEmpty(wd)) {
			String[] condition = wd.split(" ");
			for (String str : condition) {
				hql.append(" AND (borrowBook.bookBarCode LIKE ")
						.append(UtilTools.toSqlFuzzyString(str))
						.append(" OR borrowBook.bookCollection LIKE ")
						.append(UtilTools.toSqlFuzzyString(str))
						.append(" OR borrowBook.bookCallNo LIKE ")
						.append(UtilTools.toSqlFuzzyString(str))
						.append(" OR borrowBook.bookName LIKE ")
						.append(UtilTools.toSqlFuzzyString(str))
						.append(" OR borrowBook.bookChargePerson LIKE ")
						.append(UtilTools.toSqlFuzzyString(str))
						.append(" OR borrowBook.bookPublishing LIKE ")
						.append(UtilTools.toSqlFuzzyString(str))
						.append(" OR borrowBook.ISBN LIKE ")
						.append(UtilTools.toSqlFuzzyString(str))
						.append(" OR borrowBook.periNo LIKE ")
						.append(UtilTools.toSqlFuzzyString(str)).append(")");
			}
		}
		Query query = session.createQuery(hql.toString());
		int c = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(pageSize);
		List<?> lstResult = query.list();
		Map<Object, Object> mapResult = new HashMap<>();
		mapResult.put("queryResult", lstResult);
		mapResult.put("count", c);

		return mapResult;

	}

	public void returnBook(int bid) {
		Session session = sessionFactory.getCurrentSession();

		String sql = "FROM BorrowBean WHERE isReturn=0 AND borrowBook.bookId=:bid";
		Query query = session.createQuery(sql);
		query.setInteger("bid", bid);
		if (query.list().size() > 0) {
			BorrowBean borrowBean = (BorrowBean) query.list().get(0);
			BooksBean book = borrowBean.getBorrowBook();
			book.setBookCount(book.getBookCount() + 1);
			book.setBookLoanCount(book.getBookLoanCount() - 1);
			session.update(book);
			borrowBean.setIsReturn(1);
			borrowBean.setReturnTime(UtilTools.receiveNowTime());
			session.update(borrowBean);
		}
	}

	public Map showMe(ManagerInfoBean manager) {
		Session session = sessionFactory.getCurrentSession();
		Map map = new HashMap<>();
		Query query = session
				.createQuery("FROM BorrowBean WHERE isReturn=0 AND borrowManager.manId="
						+ manager.getManId());
		int borCount = query.list().size();
		map.put("borCount", borCount);
		return map;
	}

	public Map myBorrow(int start, int pageSize, ManagerInfoBean manager) {
		Session session = sessionFactory.getCurrentSession();

		StringBuffer hql = new StringBuffer(
				"FROM BorrowBean WHERE borrowManager.manId="
						+ manager.getManId()
						+ " ORDER BY isReturn,borrowTime desc");

		Query query = session.createQuery(hql.toString());
		int c = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(pageSize);
		List<?> lstResult = query.list();
		Map<Object, Object> mapResult = new HashMap<>();
		mapResult.put("queryResult", lstResult);
		mapResult.put("count", c);

		return mapResult;
	}

}
